<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Controller class implementation
 */
abstract class Svc_Controller
{
    /**
     * Stores a reference to the application instance.
     *
     * @var Svc_Application
     */
    protected $application = null;

    /**
     * Stores the view payload.
     *
     * @var stdClass
     */
    protected $view = null;

    /**
     * Stores the template payload.
     *
     * @var stdClass
     */
    protected $template = null;

    /**
     * Stores the request instance.
     *
     * @var Svc_Request
     */
    private $_request = null;

    /**
     * Stores the response instance.
     *
     * @var Svc_Response
     */
    private $_response = null;

    /**
     * Initializes the controller class (all actions).
     *
     * @return
     * @internal param string $action
     */
    public abstract function init();

    /**
     * Constructs the controller class.
     *
     * @param Svc_Application $app
     * @param Svc_Request $request
     * @param Svc_Response $response
     *
     * @return Svc_Controller
     */
    public function __construct(&$app, &$request, &$response)
    {
        $this->view = new stdClass();
        $this->template = new stdClass();
        $this->application = & $app;
        $this->_request = $request;
        $this->_response = $response;
    }

    /**
     * Retrieves the view payload.
     *
     * @return stdClass
     */
    public function & getViewContent()
    {
        return $this->view;
    }

    /**
     * Retrieves the view payload.
     *
     * @return stdClass
     */
    public function & getTemplateContent()
    {
        return $this->template;
    }

    /**
     * Returns the request class instance.
     *
     * @return Svc_Request
     */
    public function & getRequest()
    {
        return $this->_request;
    }

    /**
     * Returns the response class instance.
     *
     * @return Svc_Response
     */
    public function & getResponse()
    {
        return $this->_response;
    }

    /**
     * Returns the session class instance.
     *
     * @return Svc_SessionManager
     */
    public function & getSession()
    {
        return Svc_Application::instance()->getSession();
    }

    /**
     * Returns the cookies class instance.
     *
     * @return Svc_CookieManager
     */
    public function & getCookies()
    {
        return Svc_Application::instance()->getCookies();
    }

    /**
     * Returns the cookies class instance.
     *
     * @return Svc_Translator
     */
    public function & getTranslator()
    {
        return Svc_Application::instance()->getTranslator();
    }

    /**
     * Returns a view response
     *
     * @param string $viewScript View script (without extension)
     * @param string $templateScript Template script (without extension)

     * @return \Svc_Response_View
     */
    public function view($viewScript = null, $templateScript = 'template')
    {
        return new Svc_Response_View($templateScript, $viewScript);
    }

    /**
     * Returns a JSON response
     *
     * @param null $data Json content

     * @return \Svc_Response_Json
     */
    public function json($data = null)
    {
        return new Svc_Response_Json($data);
    }

    /**
     * Returns a JSON response
     *
     * @param null $data Json content

     * @return \Svc_Response_Json
     */
    public function redirect($controller, $action = null, $parameters = null)
    {
        return new Svc_Response_Redirect($controller, $action, $parameters);
    }
}
